﻿using MediatR;
using WeatherHistory.Data.Repositories.Interfaces;
using WeatherHistory.Shared.Exceptions;

namespace WeatherHistory.Api.CommandsQueries.Commands.Accounts.Delete;

public class DeleteAccountsCommandHandler : IRequestHandler<DeleteAccountsCommandRequest>
{
    private readonly IAccountRepository _accountRepository;

    public DeleteAccountsCommandHandler(IAccountRepository accountRepository)
    {
        _accountRepository = accountRepository;
    }

    public async Task Handle(DeleteAccountsCommandRequest request, CancellationToken cancellationToken)
    {
        var account = await _accountRepository.FirstOrDefaultAsync(x => x.Id == request.AccountId, cancellationToken);
        if (account == null) throw new BadRequestException($"Аккаунт с id '{request.AccountId}' не существует");

        await _accountRepository.DeleteAsync(account, cancellationToken);
    }
}